﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Text.RegularExpressions;

namespace Web.Page
{
    public partial class XSHTGL_Add : BasePage
    {
        protected string op;

        protected void Page_Load(object sender, EventArgs e)
        {          

            if (!IsPostBack)
            {        
                this.tbRQ.Attributes.Add("onclick", "show()");
                this.tbRQ0.Attributes.Add("onclick", "show()");
                initValue();
            }
        }

        private void initValue()
        {
            // 合同联系人
            //XSHTGL_Dao daoDep = new XSHTGL_Dao();
            //List<XSHT> lsktlxr = daoDep.QueryAll();
            //this.ddlktlxr.DataSource = lsktlxr;
            //this.ddlktlxr.DataTextField = "KHLXR";
            //this.ddlktlxr.DataValueField = "ID";
            //this.ddlktlxr.DataBind();
            //this.ddlktlxr.Items.Insert(0, new ListItem("请选择", "0"));                  
            // 销售部门ID     
            // 部门
            Departments_Dao daoDep1 = new Departments_Dao();
            List<Department> lsDep = daoDep1.QueryAll();
            this.ddlxsbmID.DataSource = lsDep;
            this.ddlxsbmID.DataTextField = "Name";
            this.ddlxsbmID.DataValueField = "ID";
            this.ddlxsbmID.DataBind();
            this.ddlxsbmID.Items.Insert(0, new ListItem("请选择", "0"));                  

            //出货方式
            //List<XSHT> lschfs = daoDep.QueryAll();
            //this.ddlchfs.DataSource = lschfs;
            //this.ddlchfs.DataTextField = "CHFS";
            //this.ddlchfs.DataValueField = "ID";
            //this.ddlchfs.DataBind();
        }


        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {     
            this.lbResult.Text = "";        

            if (this.ddlktbh.Text.Trim()=="")
            {
                this.lbResult.Text = "请输入或选择合同编号";
                return;
            }
            if (this.ddlktmc.Text.Trim() == "")
            {
                this.lbResult.Text = "请输入或选择合同名称";
                return;
            }
            if (this.ddlktlxr.Text.Trim() == "")
            {
                this.lbResult.Text = "请输入或选择合同联系人";
                return;
            }
            if (this.ddlchfs.Text.Trim() == "")
            {
                this.lbResult.Text = "请输入或选择出货方式";
                return;
            }
            if (this.tbRQ.Text.Equals("0"))
            {
                this.lbResult.Text = "请输入或选择出货时间";
                return;
            }
            if (this.tbRQ0.Text.Equals("0"))
            {
                this.lbResult.Text = "请输入或选择到货时间";
                return;
            }
            
            string id=Guid.NewGuid().ToString();
            XSHT entity = new XSHT()
            {
                ID = Guid.Parse(id),
                BH = this.ddlktbh.Text.Trim(),
                KHMC = this.ddlktmc.Text,
                KHLXR = this.ddlktlxr.Text,
                XSBMID = Guid.Parse(this.ddlxsbmID.SelectedValue.Trim()),
                XSRYID = Guid.Parse(this.ddlktlxrid.SelectedValue.Trim()),
                DHSJ = DateTime.Parse(this.tbRQ0.Text.Trim()),
                CHSJ = DateTime.Parse(this.tbRQ.Text.Trim()),
                CHFS = this.ddlchfs.Text.Trim(),
                HTZT = int.Parse(this.ddlhtzt.SelectedValue)
            };

           
            string resultMessage = "";
            try
            {
                XSHTGL_Dao dao = new XSHTGL_Dao();
                int result = dao.Add(entity);
                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {           
           this.ddlktbh.Text = "";
           this.ddlktmc.Text = "";
           this.ddlktlxr.Text = "";
           this.ddlxsbmID.SelectedIndex = 0;
           this.ddlktlxrid.SelectedIndex = 0;
           this.tbRQ0.Text = "";
           this.tbRQ.Text = "";
           this.ddlchfs.Text = "";
           this.ddlhtzt.Text = "";         

           this.lbResult.Text = "";
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/XSHTGL_Query.aspx?op=" + this.hdId.Value);
        }

        protected class UserNames
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
        protected void ddlxsbmID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlktlxrid.Items.Clear();
            string depId = ddlxsbmID.SelectedValue;
            if (depId != "0")
            {
                Guid guid = Guid.Parse(depId);
                using (var context = new Dao.ErpEntities())
                {
                    var query = from d in context.UserInDepartmentSet
                                join u in context.UserSet
                                on d.UserId equals u.Id
                                where d.DepartmentId == guid
                                select new
                                {
                                    Id = d.UserId,
                                    Name = u.Name
                                };
                    List<UserNames> list = new List<UserNames>();
                    if (query.Count() > 0)
                    {
                        foreach (var q in query)
                        {
                            UserNames un = new UserNames();
                            un.Id = q.Id.ToString();
                            un.Name = q.Name;
                            list.Add(un);
                        }
                    }

                    this.ddlktlxrid.DataSource = list;
                    this.ddlktlxrid.DataTextField = "Name";
                    this.ddlktlxrid.DataValueField = "Id";
                    this.ddlktlxrid.DataBind();
                }
            }

            this.ddlktlxrid.Items.Insert(0, new ListItem("请选择", "0"));
        }
    }
}